#!/usr/bin/perl

#=======================================================================
# lpar
# File ID: 5915171c-8768-11e0-9a68-00023faf1383
# Add lpar info of current repo to lpar.git/$1.lpar
#
# Character set: UTF-8
# ©opyleft 2011– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use warnings;
use Getopt::Long;
use Cwd qw{ abs_path getcwd };
use File::Path qw{ make_path };

local $| = 1;

our $Debug = 0;

our %Opt = (

    'all' => 0,
    'debug' => 0,
    'help' => 0,
    'set' => undef,
    'verbose' => 0,
    'version' => 0,

);

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = '0.00';
my $MAX_NAME_LENGTH = 245;
my $allowed_str = "Allowed characters: A-Z, a-z, 0-9, '-', '.', '/' and '_'.\n" .
                  "Maximum length: $MAX_NAME_LENGTH chars.\n";

Getopt::Long::Configure('bundling');
GetOptions(

    'all|a' => \$Opt{'all'},
    'debug' => \$Opt{'debug'},
    'help|h' => \$Opt{'help'},
    'set|s=s' => \$Opt{'set'},
    'verbose|v+' => \$Opt{'verbose'},
    'version' => \$Opt{'version'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

if (get_config_value('lpar.skip') eq 'true') {
    msg(0, "lpar.skip is 'true', skip lpar in " . getcwd());
    exit(0);
}

if (defined($Opt{'set'}) && $Opt{'set'} eq '?') {
    printf("%s\n", get_config_value('lpar.name'));
    exit(0);
}

$Opt{'all'} || system("lpar -a");

if (defined($Opt{'set'})) {
    my $name = $Opt{'set'};
    my $prev = get_config_value('lpar.name');
    if (length($name)) {
        if (valid_repo_name($name)) {
            if ($name eq $prev) {
                msg(0, "Value of lpar.name is already '$name', doing nothing");
                exit(1);
            } else {
                msg(0, "Changing value of lpar.name from '$prev' to '$name'");
                exec("git", "config", "lpar.name", $name);
            }
        } else {
            die("$progname: Invalid value in -s/--set argument\n$allowed_str");
        }
    } else {
        msg(0, "Deleting lpar.name config variable, previous value was '$prev'");
        exec("git", "config", "--unset", "lpar.name");
    }
}

my $repo = $Opt{'all'} ? 'all' : get_config_value("lpar.name");
length($repo) || die("$progname: lpar.name not defined\n");
if (!valid_repo_name($repo)) {
    die("$progname: lpar.name contains illegal value\n$allowed_str");
}

my $lpar_dir = $Opt{'all'} ? "$ENV{'HOME'}/src/git/all-lpar" : "$ENV{'HOME'}/src/git/lpar";
my $lpar_file = "$lpar_dir/$repo.lpar.new";
-d $lpar_dir || make_path($lpar_dir);

my @shas = scalar(@ARGV) ? @ARGV : ();
lock_file($lpar_file);
if (scalar(@shas)) {
    for my $curr (@shas) {
        chomp(my $sha1 = `git log -1 --format=%H "$curr" 2>/dev/null`);
        length($sha1) || die("$progname: $curr: Invalid ref, must point to a commit\n");
        system("git lpar `git branch -a --contains=\"$sha1\" | cut -c3- | grep -v -e '->' -e '(no branch)'` >>$lpar_file");
    }
} else {
    system("git lpar --all >>$lpar_file");
}
system("git for-each-ref >>$lpar_file");
unlock_file($lpar_file);

sub lock_file {
    # {{{
    my $file = shift;
    my $lockdir = "$file.lock";
    my $did_lock = 0;
    until (mkdir($lockdir)) {
        print(STDERR "$progname: $file: File is locked, waiting for access...\n");
        $did_lock = 1;
        sleep(1);
    }
    $did_lock && print(STDERR "$progname: $file: Obtained lock\n");
    return;
    # }}}
} # lock_file()

sub unlock_file {
    # {{{
    my $file = shift;
    my $lockdir = "$file.lock";
    rmdir($lockdir) || warn("$progname: $lockdir: Lockdir unexpectedly disappeared");
    return;
    # }}}
} # unlock_file()

sub valid_repo_name {
    # {{{
    my $repo = shift;
    my $retval = ($repo =~ /[^\-\.\/0-9A-Z_a-z]/) ? 0 : 1;
    (!length($repo) || length($repo) > $MAX_NAME_LENGTH) && ($retval = 0);
    return($retval);
    # }}}
} # valid_repo_name()

sub get_config_value {
    # {{{
    my $name = shift;
    my $retval = '';
    chomp($retval = `git config --get "$name"`);
    return($retval);
    # }}}
} # get_config_value()

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    return;
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<"END");

Usage: $progname [options] [included_SHA [...]]

Options:

  -a, --all
    Dump everything into a single file, ~/src/git/all-lpar/all.lpar .
  -h, --help
    Show this help.
  -s X, --set X
    Set the lpar.name variable to X in git config.
    To delete, specify "". A value of "?" will print the current value. 
    To avoid shell expansion, prefix the question mark with a backslash 
    ("\\") or specify it as "-s?" or "--set=?".
  -v, --verbose
    Increase level of verbosity. Can be repeated.
  --version
    Print version information.
  --debug
    Print debugging messages.

If the Git configuration variable lpar.skip is set to 'true' in a 
specific repository, the command is ignored. This is useful when dealing 
with shallow clones or grafts where parents or root commits are 
different from the original repository.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    return;
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return('');
    # }}}
} # D()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME



=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
